package com.bdqn.zhang.servlet;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.zhang.pojo.User;
import com.bdqn.zhang.service.UserService;
import com.bdqn.zhang.service.impl.UserServiceImpl;
import com.bdqn.zhang.utils.DateUtils;
import com.bdqn.zhang.utils.LayData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "userServlet", urlPatterns = {"/system/user"}, loadOnStartup = 1)
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        UserService userService = new UserServiceImpl();
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        //这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
        response.setCharacterEncoding("UTF-8");
        if("open".equals(opr)){
            request.getRequestDispatcher("/WEB-INF/user/user.html").forward(request,response);
        } else if("list".equals(opr)){
            List<User> userlist = userService.findAll();
            int count = userService.count();
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            LayData laydata = LayData.data(count, userlist);
            String result = JSONObject.toJSONString(laydata);
            out.print(result);
        } else if("add".equals(opr)){
            request.getRequestDispatcher("/WEB-INF/user/useradd.html").forward(request,response);
        } else if("addSave".equals(opr)){
            User u = new User();
            u.setUsername(request.getParameter("username"));
            u.setPassword(request.getParameter("password"));
            u.setAddress(request.getParameter("address"));
            u.setCreateby(request.getParameter("createBy"));
            u.setCreatedate(DateUtils.parseDate(request.getParameter("createdate")));
            int count = userService.addUser(u);
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        } else if("edit".equals(opr)){
            User user = userService.findUserById(Integer.parseInt(request.getParameter("id")));
            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher("/WEB-INF/user/useredit.html").forward(request,response);
        } else if("editShowData".equals(opr)){
            User user = (User)request.getSession().getAttribute("user");
            request.getSession().removeAttribute("user");
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONStringWithDateFormat(user,"yyyy-MM-dd HH:mm:ss"));
        } else if("editSave".equals(opr)){
            User u = new User();
            u.setId(Integer.parseInt(request.getParameter("id")));
            u.setUsername(request.getParameter("username"));
            u.setPassword(request.getParameter("password"));
            u.setAddress(request.getParameter("address"));
            u.setCreateby(request.getParameter("createBy"));
            u.setCreatedate(DateUtils.parseDate(request.getParameter("createdate")));
            int count = userService.updateUser(u);
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        } else if("detail".equals(opr)){
            User user = userService.findUserById(Integer.parseInt(request.getParameter("id")));
            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher("/WEB-INF/user/userdetail.html").forward(request,response);
        } else if("detailShowData".equals(opr)){
            User user = (User)request.getSession().getAttribute("user");
            request.getSession().removeAttribute("user");
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONStringWithDateFormat(user,"yyyy-MM-dd HH:mm:ss"));
        } else if("removeAll".equals(opr)){
            int count = userService.removeUsers(request.getParameter("ids"));
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        } else if("del".equals(opr)){
            int count = userService.delUser(Integer.parseInt(request.getParameter("id")));
            PrintWriter out = response.getWriter();
            out.print(JSONObject.toJSONString(count));
        }
    }
}
